<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\WithdrawalSetting as ModelWithdrawalSetting;
use app\common\model\WithdrawalSettings;

/**
 * 提现设置
 *
 * @icon fa fa-circle-o
 */
class WithdrawalSetting extends Backend
{

    /**
     * WithdrawalSetting模型对象
     * @var \app\common\model\WithdrawalSetting
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\WithdrawalSetting;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['type'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->clearing_form = ($row->clearing_form == 1) ? '秒结算' : '月结算';
                $row->is_open = ($row->is_open == 1) ? '开启' : '关闭';
                $row->getRelation('type')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function closed()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isGet()) {
            $ids = $this->request->get('ids');
            $ids = json_decode($ids);
            $this->view->assign('ids', implode(",", $ids));
            return $this->view->fetch();
        }
        if ($this->request->isPost()) {
            $ids = $this->request->request('ids');
            $is_open = $this->request->request('is_open');
            $remarks = $this->request->request('remarks');
            $data = [
                "is_open" => $is_open
            ];
            if (!empty($remarks)) {
                $data["remarks"] = $remarks;
            }
            $res = ModelWithdrawalSetting::where(['id' => ["in", $ids]])->update($data);
            if ($res) {
                $this->success("保存成功");
            } else {
                $this->error("保存失败");
            }
        }
    }

    public function settings()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isGet()) {
            $setting = WithdrawalSettings::where(["status" => 1])->find();
            $this->view->assign('row', $setting);
            return $this->view->fetch();
        }
        if ($this->request->isPost()) {
            $id = $this->request->request('id');
            $row = $this->request->request('row/a');
            $data = [
                "check_type" => $row["check_type"],
                "clearing_form" => $row["clearing_form"],
                "is_open" => $row["is_open"],
                "withdrawal_start_time" => $row["withdrawal_start_time"],
                "withdrawal_end_time" => $row["withdrawal_end_time"],
                "withdrawal_min_money" => $row["withdrawal_min_money"],
                "withdrawal_min_money" => $row["withdrawal_min_money"],
                "withdrawal_max_money" => $row["withdrawal_max_money"],
                "service_charge" => $row["service_charge"],
                "tax_rate" => $row["tax_rate"],
                "withdrawal_number" => $row["withdrawal_number"],
                "remarks" => $row["remarks"]
            ];

            $res = WithdrawalSettings::where(['id' => $id])->update($data);
            if ($res) {
                $this->success("保存成功");
            } else {
                $this->error("保存失败");
            }
        }
    }
}
